<template>
  <div class="container">
    <div class="app-container">
      <el-button size="mini" style="margin: 20px;" type="primary" @click="addPer">
        添加权限
      </el-button>
      <el-table :data="list" row-key="id" default-expand-all="">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" align="center" />
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column label="操作" align="center">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" size="mini" @click="addDialog(row)">添加</el-button>
            <el-button type="text" size="mini" @click="editPer(row.id)">编辑</el-button>
            <el-popconfirm
              title="这段内容确定删除吗？"
              style="margin-left: 10px;"
              @onConfirm="delPer(row.id)"
            >
              <el-button slot="reference" type="text" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      :title="showTitle"
      :visible="showDialog"
      @close="btnCancel"
    >
      <el-form ref="form" :model="formData" :rules="fromRules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" size="mini" />
        </el-form-item>

        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" size="mini" />
        </el-form-item>

        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" :rows="4" style="width: 90%" type="textarea" size="mini" />
        </el-form-item>

        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="btnOk">确认</el-button>
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermission, addPermission, delPermission, getPer, editPer } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: ''
      },
      fromRules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入标识名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入权限描述', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    async getPermission() {
      this.list = transListToTreeData(await getPermission(), 0)
    },
    addDialog(row) {
      this.showDialog = true
      console.log(row)
      this.formData.pid = row.id
      this.formData.type = 2
    },
    btnOk() {
      this.$refs.form.validate(async(isok) => {
        if (isok) {
          if (this.formData.id) {
            await editPer(this.formData)
            this.$message.success('编辑成功')
          } else {
            await addPermission(this.formData)
            this.$message.success('新增成功')
          }
          this.getPermission()
          this.btnCancel()
        }
      })
    },
    btnCancel() {
      this.showDialog = false
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.formData = {
          name: '',
          code: '',
          description: '',
          type: '',
          pid: '',
          enVisible: '0'
        }
        this.$refs.form.resetFields()
      }, 200)
    },
    addPer() {
      this.showDialog = true
      this.formData.type = 1
      this.formData.pid = 0
    },
    async delPer(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermission()
      this.btnCancel()
    },
    async editPer(id) {
      this.formData = await getPer(id)
      this.showDialog = true
    }
  }
}

</script>

  <style>

  </style>
